<?php
declare (strict_types = 1);

namespace app\admin\middleware;

use think\facade\Cache;
use think\facade\Db;

class Actionuser
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        //
        $handler = Cache::store('redis')->handler();

        $res = Db::name('action')->field('id, name, action, action_num')->order('id asc')->where(['status'=>1, 'is_delete'=>1])->select()->toArray();

        if($res){
            $actionData = $action = [];
            foreach($res as $val){
                $action[$val['id']] = $val['action'];
                $actionData[$val['id']] = $val['name'];
                $handler->hset('action', 'action_' . $val['id'], json_encode($val));
            }
            $handler->hset('action', 'action_-1', json_encode($action));
            $handler->hset('action', 'action_0', json_encode($actionData));
        }
        // 继续执行进入到控制器
        return $next($request);
    }
}
